<template>

  <el-dialog
    title="添加合同"
    :visible="rentvisible"
    width="580px"
    @close="closeDialog"
  >
    <!-- 表单区域 -->
    <div class="form-container">
      <el-form ref="addForm" :model="rentForm" :rules="rentRules" label-position="top">
        <el-form-item label="租赁楼宇" prop="buildingId">
          <el-select v-model="rentForm.buildingId" placeholder="请选择">
            <el-option
              v-for="item in build"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租赁起止日期" prop="rentTime">
          <el-date-picker
            v-model="rentForm.rentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="租赁合同" prop="contractId">
          <el-upload
            action="#"
            :before-upload="beforeUpload"
            :http-request="uploadHandle"
          >
            <el-button size="small" type="primary" plain>上传合同文件</el-button>
            <div slot="tip" class="el-upload__tip">支持扩展名：.doc .docx .pdf, 文件大小不超过5M</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button size="mini" type="primary" @click="confirmBtn">确 定</el-button>
    </template>
  </el-dialog>

</template>

<script>
import { uploadAPI } from '@/api/comm'
import { addRent, getBuildingList } from '@/api/rent'

export default {
  name: 'Rent',
  props: {
    rentvisible: {
      type: Boolean,
      default: false
    },
    idcurrent: {
      type: Number,
      default: null
    }
  },
  // import引入的组件需要注入到对象中才能使用
  data() {
    return {
      rentForm: {
        buildingId: null, // 楼宇id
        contractId: null, // 合同id
        contractUrl: '', // 合同Url
        enterpriseId: '', // 企业名称
        type: 0, // 合同类型
        rentTime: [] // 合同时间
      },
      build: [],
      rentRules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        rentTime: [
          { required: true, message: '请选择租赁日期', trigger: 'change' }
        ],
        contractId: [
          { required: true, message: '请上传合同文件' }
        ]
      }
    }
  },
  created() {
    this.getBuilding()
  },
  methods: {
    // 关闭弹窗
    closeDialog() {
      this.$emit('update:rentvisible', false)
      this.$refs.addForm.resetFields()
    },
    async getBuilding() {
      const { data } = await getBuildingList()
      this.build = data
    },
    // 上传验证
    beforeUpload(file) {
      // 定义文件类型列表.doc .docx .pdf
      const typeList = ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf']
      //  定义文件大小不得超过5M
      const fileSize = file.size / 1024 / 1024 < 5
      // 必需为 typeList中的类型才行
      const istypeList = typeList.includes(file.type)
      if (!istypeList) {
        this.$message.error('支持扩展名：.doc .docx .pdf!')
      }
      if (!fileSize) {
        this.$message.error('文件大小不得超过5M!')
      }
      return istypeList && fileSize
    },
    async uploadHandle(data) {
      const fd = new FormData()
      fd.append('file', data.file)
      fd.append('type', 'contract')
      const res = await uploadAPI(fd)
      this.rentForm.contractId = res.data.id
      this.rentForm.contractUrl = res.data.url
      this.$refs.addForm.validateField('contractId')
    },
    async confirmBtn() {
      await this.$refs.addForm.validate()
      this.rentForm.enterpriseId = this.idcurrent
      const { rentTime, ...formData } = this.rentForm
      const startTime = rentTime[0]
      const endTime = rentTime[1]
      await addRent({ ...formData, startTime, endTime })
      this.$emit('refresh')
      this.closeDialog()
    }
  }
}
</script>
